<?php
/**
 * Created by PhpStorm.
 * User: alinejun
 * Date: 2019/2/17
 * Time: 8:20
 */
namespace app\index\controller;

use app\admin\validate\Member;

/**
 * 前端首页控制器
 */
class Login extends IndexBase
{
    public function index()
    {
        $this->view->engine->layout(false);
        return $this->fetch('index');
    }

    public function register()
    {
        $this->view->engine->layout(false);
        return $this->fetch('register');
    }

    public function doLogin()
    {
        $username = input('post.username');
        $password = input('post.password');
        $member = $this->logicMember->getMemberInfo(['username' => $username]);
        if (!empty($member['password']) && data_md5_key($password) == $member['password']) {
            session('member_info', $member);
            session('userid', $member['id']);
            return $this->success('登录成功',  url('index/index'),2);
        } else {
            $error = empty($member['id']) ? '用户账号不存在' : '密码输入错误';
            return $this->error([$error, url('login/index'),3]);
        }
    }

    public function doRegister()
    {
        $validate = new Member();
        if(!$validate->scene('add')->check($this->param)){
            echo json_encode([RESULT_ERROR, $validate->getError()]);exit;
        }
        $member = new \app\common\model\Member();
        $this->param['leader_id'] = 1;
        $this->param['is_inside'] = 1;
        $result = $member->setInfoM( $this->param);
        $result ? $this->success('会员添加成功',url('login/index')) :     $this->error(['会员添加失败', url('login/index'),3]);;exit;
    }

    public function loginOut()
    {
        session('member_info',[]);
        session('userid','');
        $this->success('已退出~',url('index'));
    }
}